home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / ZFONT1.C < prev    next >
C/C++ Source or Header  |  1992-03-08  |  11KB  |  293 lines

  1. /* Copyright (C) 1991, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* zfont1.c */
  21. /* Type 1 font creation operator for Ghostscript */
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "gxfixed.h"
  26. #include "gsmatrix.h"
  27. #include "gxdevice.h"
  28. #include "gschar.h"
  29. #include "gxfont.h"
  30. #include "dict.h"
  31. #include "font.h"
  32. #include "name.h"
  33. #include "store.h"
  34.  
  35. /* Imported procedures from zfont2.c */
  36. extern int build_gs_simple_font(P4(os_ptr, gs_font **, font_type, ref *));
  37. extern int font_int_param(P6(ref *pdict, ref *pname, int minval, int maxval,
  38.                  int defaultval, int *pvalue));
  39.  
  40. /* Forward references */
  41. private int near font_bool_param(P4(ref *pdict, ref *pname,
  42.                     int defaultval, int *pvalue));
  43. private int near font_float_param(P4(ref *pdict, ref *pname,
  44.                      floatp defaultval, float *pvalue));
  45. private int near font_int_array(P4(ref *pdict, ref *pname,
  46.                    uint maxlen, int *ivec));
  47. private int near font_float_array(P4(ref *pdict, ref *pname,
  48.                      uint maxlen, float *fvec));
  49.  
  50. /* Type 1 auxiliary procedures (defined in zchar.c) */
  51. extern int z1_subr_proc(P3(gs_type1_data *, int, byte **));
  52. extern int z1_pop_proc(P2(gs_type1_data *, fixed *));
  53.  
  54. /* Names of system-known keys in font dictionaries: */
  55. private ref name_PaintType;
  56. extern ref name_UniqueID;        /* in zfont2 */
  57. private ref name_Type1BuildChar;
  58. private ref name_Private;
  59. private ref name_CharStrings;        /* only needed for seac */
  60. /* Names of system-known keys in type 1 font Private dictionary: */
  61. private ref name_BlueFuzz;
  62. private ref name_BlueScale;
  63. private ref name_BlueShift;
  64. private ref name_BlueValues;
  65. private ref name_ExpansionFactor;
  66. private ref name_FamilyBlues;
  67. private ref name_FamilyOtherBlues;
  68. private ref name_ForceBold;
  69. private ref name_LanguageGroup;
  70. private ref name_lenIV;
  71. private ref name_OtherBlues;
  72. private ref name_RndStemUp;
  73. private ref name_StdHW;
  74. private ref name_StdVW;
  75. private ref name_StemSnapH;
  76. private ref name_StemSnapV;
  77. private ref name_Subrs;
  78.  
  79. /* Default value of lenIV */
  80. #define default_lenIV 4
  81.  
  82. /* Initialize the font building operators */
  83. private void
  84. zfont1_init()
  85. {    static names_def fnd1[] = {
  86.  
  87.     /* Create the names of the standard elements of */
  88.     /* a font dictionary. */
  89.        { "PaintType", &name_PaintType },
  90.        { "Type1BuildChar", &name_Type1BuildChar },
  91.        { "Private", &name_Private },
  92.        { "CharStrings", &name_CharStrings },
  93.  
  94.     /* Create the names of the known entries in */
  95.     /* a type 1 font Private dictionary. */
  96.        { "BlueFuzz", &name_BlueFuzz },
  97.        { "BlueScale", &name_BlueScale },
  98.        { "BlueShift", &name_BlueShift },
  99.        { "BlueValues", &name_BlueValues },
  100.        { "ExpansionFactor", &name_ExpansionFactor },
  101.        { "FamilyBlues", &name_FamilyBlues },
  102.        { "FamilyOtherBlues", &name_FamilyOtherBlues },
  103.        { "ForceBold", &name_ForceBold },
  104.        { "LanguageGroup", &name_LanguageGroup },
  105.        { "lenIV", &name_lenIV },
  106.        { "OtherBlues", &name_OtherBlues },
  107.        { "RndStemUp", &name_RndStemUp },
  108.        { "StdHW", &name_StdHW },
  109.        { "StdVW", &name_StdVW },
  110.        { "StemSnapH", &name_StemSnapH },
  111.        { "StemSnapV", &name_StemSnapV },
  112.        { "Subrs", &name_Subrs },
  113.  
  114.     /* Mark the end of the initalized name list. */
  115.        names_def_end
  116.     };
  117.  
  118.     init_names(fnd1);
  119.  
  120.     /* Make the standard BuildChar procedures executable. */
  121.     r_set_attrs(&name_Type1BuildChar, a_executable);
  122. }
  123.  
  124. /* .buildfont1 */
  125. /* Build a type 1 (Adobe encrypted) font. */
  126. int
  127. zbuildfont1(os_ptr op)
  128. {    gs_type1_data data1;
  129.     ref *psubrs;
  130.     ref *pcharstrings;
  131.     ref *pprivate;
  132.     ref *puniqueid;
  133.     static ref no_subrs;
  134.     gs_font *pfont;
  135.     font_data *pdata;
  136.     int code;
  137.     check_type(*op, t_dictionary);
  138.     code = font_int_param(op, &name_PaintType, 0, 2, 0, &data1.PaintType);
  139.     if ( code < 0 ) return 0;
  140.     if ( dict_find(op, &name_CharStrings, &pcharstrings) <= 0 ||
  141.         !r_has_type(pcharstrings, t_dictionary) ||
  142.         dict_find(op, &name_Private, &pprivate) <= 0 ||
  143.         !r_has_type(pprivate, t_dictionary)
  144.        )
  145.         return e_invalidfont;
  146.     if ( dict_find(pprivate, &name_Subrs, &psubrs) > 0 )
  147.        {    check_array_else(*psubrs, e_invalidfont);
  148.        }
  149.     else
  150.        {    make_tasv(&no_subrs, t_string, 0, 0, bytes, (byte *)0),
  151.         psubrs = &no_subrs;
  152.        }
  153.     /* Get the rest of the information from the Private dictionary. */
  154.     if ( (code = font_int_param(pprivate, &name_lenIV, 0, 255,
  155.                     default_lenIV, &data1.lenIV)) < 0 ||
  156.          (code = font_int_param(pprivate, &name_BlueFuzz, 0, 1999, 1,
  157.                     &data1.BlueFuzz)) < 0 ||
  158.          (code = font_float_param(pprivate, &name_BlueScale, 0.04379,
  159.                       &data1.BlueScale)) < 0 ||
  160.          (code = font_int_param(pprivate, &name_BlueShift, 0, 1999, 7,
  161.                     &data1.BlueShift)) < 0 ||
  162.          (code = data1.BlueValues.count = font_int_array(pprivate,
  163.         &name_BlueValues, max_BlueValues * 2,
  164.         &data1.BlueValues.data[0])) < 0 ||
  165.          (code = font_float_param(pprivate, &name_ExpansionFactor, 0.06,
  166.                       &data1.ExpansionFactor)) < 0 ||
  167.          (code = data1.FamilyBlues.count = font_int_array(pprivate,
  168.         &name_FamilyBlues, max_FamilyBlues * 2,
  169.         &data1.FamilyBlues.data[0])) < 0 ||
  170.          (code = data1.FamilyOtherBlues.count = font_int_array(pprivate,
  171.         &name_FamilyOtherBlues, max_FamilyOtherBlues * 2,
  172.         &data1.FamilyOtherBlues.data[0])) < 0 ||
  173.          (code = font_bool_param(pprivate, &name_ForceBold, 0,
  174.                      &data1.ForceBold)) < 0 ||
  175.          (code = font_int_param(pprivate, &name_LanguageGroup, 0, 1, 0,
  176.                     &data1.LanguageGroup)) < 0 ||
  177.          (code = data1.OtherBlues.count = font_int_array(pprivate,
  178.         &name_OtherBlues, max_OtherBlues * 2,
  179.         &data1.OtherBlues.data[0])) < 0 ||
  180.          (code = font_bool_param(pprivate, &name_RndStemUp, 0,
  181.                      &data1.RndStemUp)) < 0 ||
  182.          (code = data1.StdHW.count = font_float_array(pprivate,
  183.         &name_StdHW, 1, &data1.StdHW.data[0])) < 0 ||
  184.          (code = data1.StdVW.count = font_float_array(pprivate,
  185.         &name_StdVW, 1, &data1.StdVW.data[0])) < 0 ||
  186.          (code = data1.StemSnapH.count = font_float_array(pprivate,
  187.         &name_StemSnapH, max_StemSnap,
  188.         &data1.StemSnapH.data[0])) < 0 ||
  189.          (code = data1.StemSnapV.count = font_float_array(pprivate,
  190.         &name_StemSnapV, max_StemSnap,
  191.         &data1.StemSnapV.data[0])) < 0
  192.        )
  193.         return code;
  194.     /* Do the work common to all non-composite font types. */
  195.     code = build_gs_simple_font(op, &pfont, ft_encrypted, &name_Type1BuildChar);
  196.     if ( code != 0 ) return code;
  197.     /* This is a new font, fill it in. */
  198.     pdata = (font_data *)pfont->client_data;
  199.     pfont->data.base.type1_data = data1;
  200.     ref_assign(&pdata->CharStrings, pcharstrings);
  201.     ref_assign(&pdata->Subrs, psubrs);
  202.     pfont->data.base.type1_data.subr_proc = z1_subr_proc;
  203.     pfont->data.base.type1_data.pop_proc = z1_pop_proc;
  204.     pfont->data.base.type1_data.proc_data = (char *)pdata;
  205.     /* Check that the UniqueIDs match.  This is part of the */
  206.     /* Adobe protection scheme, but we may as well emulate it. */
  207.     if ( pfont->data.base.UniqueID >= 0 )
  208.        {    if ( dict_find(pprivate, &name_UniqueID, &puniqueid) <= 0 ||
  209.              !r_has_type(puniqueid, t_integer) ||
  210.              puniqueid->value.intval != pfont->data.base.UniqueID
  211.            )
  212.             pfont->data.base.UniqueID = -1;
  213.        }
  214.     return 0;
  215. }
  216.  
  217. /* ------ Initialization procedure ------ */
  218.  
  219. op_def zfont1_op_defs[] = {
  220.     {"1.buildfont1", zbuildfont1},
  221.     op_def_end(zfont1_init)
  222. };
  223.  
  224. /* ------ Subroutines ------ */
  225.  
  226. /* Get a Boolean parameter from a font-related dictionary. */
  227. /* Return 0 if found, 1 if defaulted, <0 if wrong type. */
  228. private int near
  229. font_bool_param(ref *pdict, ref *pname, int defaultval, int *pvalue)
  230. {    ref *pdval;
  231.     if ( dict_find(pdict, pname, &pdval) <= 0 )
  232.        {    *pvalue = defaultval;
  233.         return 1;
  234.        }
  235.     if ( !r_has_type(pdval, t_boolean) ) return e_typecheck;
  236.     *pvalue = pdval->value.index;
  237.     return 0;
  238. }        
  239.  
  240. /* Get a float parameter from a font-related dictionary. */
  241. /* Return 0 if found, 1 if defaulted, <0 if wrong type. */
  242. private int near
  243. font_float_param(ref *pdict, ref *pname, floatp defaultval, float *pvalue)
  244. {    ref *pdval;
  245.     if ( dict_find(pdict, pname, &pdval) <= 0 )
  246.        {    *pvalue = defaultval;
  247.         return 1;
  248.        }
  249.     switch ( r_type(pdval) )
  250.        {
  251.     case t_integer: *pvalue = pdval->value.intval; return 0;
  252.     case t_real: *pvalue = pdval->value.realval; return 0;
  253.        }
  254.     return e_typecheck;
  255. }        
  256.  
  257. /* Get an integer array from a Type 1 font Private dictionary. */
  258. /* Return the element count if OK, 0 if missing, <0 if invalid. */
  259. private int near
  260. font_int_array(ref *pdict, ref *pname, uint maxlen, int *ivec)
  261. {    ref *pdval, *pa;
  262.     int *pi = ivec;
  263.     uint size;
  264.     int i;
  265.     if ( dict_find(pdict, pname, &pdval) <= 0 ) return 0;
  266.     if ( !r_has_type(pdval, t_array) ) return e_invalidfont;
  267.     size = r_size(pdval);
  268.     if ( size > maxlen ) return e_limitcheck;
  269.     pa = pdval->value.refs;
  270.     for ( i = 0; i < size; i++, pa++, pi++ )
  271.        {    if ( !r_has_type(pa, t_integer) ) return e_typecheck;
  272.         if ( pa->value.intval != (int)pa->value.intval )
  273.             return e_rangecheck;
  274.         *pi = (int)pa->value.intval;
  275.        }
  276.     return size;
  277. }        
  278.  
  279. /* Get a float array from a Type 1 font Private dictionary. */
  280. /* Return the element count if OK, 0 if missing, <0 if invalid. */
  281. private int near
  282. font_float_array(ref *pdict, ref *pname, uint maxlen, float *fvec)
  283. {    ref *pdval;
  284.     uint size;
  285.     int code;
  286.     if ( dict_find(pdict, pname, &pdval) <= 0 ) return 0;
  287.     if ( !r_has_type(pdval, t_array) ) return e_invalidfont;
  288.     size = r_size(pdval);
  289.     if ( size > maxlen ) return e_limitcheck;
  290.     code = num_params(pdval->value.refs + size - 1, size, fvec);
  291.     return (code >= 0 ? size : code);
  292. }        
  293.